package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.Menu;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * 视频分类convert
 * Created by wangjian on 2015-11-09.
 */
public class MenuConvert {

    public static DBObject castMenuToDBObject(Menu menu) {
        DBObject dbo = new BasicDBObject();
        if (menu.getMenuId() != null) {
            dbo.put("_id", menu.getMenuId());
        }
        dbo.put("menuName",menu.getMenuName());
        dbo.put("command", menu.getCommand());
        dbo.put("status",menu.getStatus());
        dbo.put("type",ObjectUtils.toInteger(menu.getType(), 1));
        dbo.put("createTime", ObjectUtils.toLong(menu.getCreateTime(), System.currentTimeMillis()));
        dbo.put("orderNum", ObjectUtils.toInteger(menu.getOrderNum(), 0));
        if(menu.getMenuPicture()!=null){
            dbo.put("menuPicture", ObjectUtils.toString(menu.getMenuPicture()));
        }

        return dbo;
    }

    public static Menu castDBObjectToMenu(DBObject dbObject) {
        Menu menu = new Menu();
        if(dbObject != null) {
            menu.setMenuId(ObjectUtils.toLong(dbObject.get("_id")));
            menu.setMenuName(ObjectUtils.toString(dbObject.get("menuName"), ""));
            menu.setCommand(ObjectUtils.toString(dbObject.get("command"), ""));
            menu.setStatus(ObjectUtils.toInteger(dbObject.get("status")));
            menu.setType(ObjectUtils.toInteger(dbObject.get("type")));
            menu.setPriority(ObjectUtils.toLong(dbObject.get("priority")));
            menu.setCreateTime(ObjectUtils.toLong(dbObject.get("createTime"), 0L));
            menu.setOrderNum(ObjectUtils.toInteger(dbObject.get("orderNum"), 0));
            menu.setMenuPicture(ObjectUtils.toString(dbObject.get("menuPicture"),""));
            return menu;
        }else{
            return null;
        }

    }
}
